local _NAME, _NS = ...
local Butsu = _G[_NAME]

local font = "interface\\addons\\sharedmedia\\fonts\\font.ttf"
local fontsize = 8
local fontflag = "outlinemonochrome"

local slots = {}
_NS.slots = slots

local OnEnter = function(self)
	local slot = self:GetID()
	if(GetLootSlotType(slot) == LOOT_SLOT_ITEM) then
		GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
		GameTooltip:SetLootItem(slot)
		CursorUpdate(self)
	end
	if(self.drop:IsShown()) then
		local r, g, b = self.drop:GetVertexColor()
		self.drop:SetVertexColor(r , g , b)
	else
		self.drop:SetVertexColor(.8, .8, .8, 0)
	end

	self.drop:Show()
end

-- local OnLeave = function(self)
	-- if(self.quality > 1) then
		-- local color = ITEM_QUALITY_COLORS[self.quality]
		-- self.drop:SetVertexColor(color.r, color.g, color.b)
	-- elseif(self.isQuestItem) then
		-- self.drop:SetVertexColor(1, 1, .2)
	-- else
		-- self.drop:Hide()
	-- end

	-- GameTooltip:Hide()
	-- ResetCursor()
-- end

local OnClick = function(self)
	if(IsModifiedClick()) then
		HandleModifiedItemClick(GetLootSlotLink(self:GetID()))
	else
		StaticPopup_Hide"CONFIRM_LOOT_DISTRIBUTION"

		LootFrame.selectedLootButton = self
		LootFrame.selectedSlot = self:GetID()
		LootFrame.selectedQuality = self.quality
		LootFrame.selectedItemName = self.name:GetText()

		LootSlot(self:GetID())
	end
end

local OnUpdate = function(self)
	if(GameTooltip:IsOwned(self)) then
		GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
		GameTooltip:SetLootItem(self:GetID())
		CursorOnUpdate(self)
	end
end

function _NS.CreateSlot(id)
	local db = _NS.db

	local iconSize = 28
	local fontSizeItem = 8
	local fontSizeCount = 8
	local fontItem = GameFontWhite:GetFont()
	local fontCount = NumberFontNormalSmall:GetFont()

	local frame = CreateFrame("Button", 'ButsuSlot'..id, Butsu)
	frame:SetHeight(math.max(fontSizeItem, 28))
	frame:SetID(id)
	frame:SetBackdrop{
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", tile = false, tileSize = 16,
		edgeFile = "Interface\\ChatFrame\\ChatFrameBackground", edgeSize = 1,
		insets = {left = 0, right = 0, top = 0, bottom = 0},
	}
	frame:SetBackdropColor(0, 0, 0, .5)
	frame:SetBackdropBorderColor(0,0,0,1)
	frame:RegisterForClicks("LeftButtonUp", "RightButtonUp")

	frame:SetScript("OnEnter", OnEnter)
	frame:SetScript("OnLeave", OnLeave)
	frame:SetScript("OnClick", OnClick)
	frame:SetScript("OnUpdate", OnUpdate)

	local iconFrame = CreateFrame("Frame", nil, frame)
	iconFrame:SetSize(28, 28)
	iconFrame:SetPoint("RIGHT", frame)
	frame.iconFrame = iconFrame

	local icon = iconFrame:CreateTexture(nil, "ARTWORK")
	icon:SetAlpha(.8)
	icon:SetTexCoord(.07, .93, .07, .93)
	--icon:SetAllPoints(iconFrame)
	icon:SetPoint("TOPLEFT", 1,-1)
	icon:SetPoint("BOTTOMRIGHT", -1,1)
	frame.icon = icon

	local quest = iconFrame:CreateTexture(nil, 'OVERLAY')
	quest:SetTexture([[Interface\Minimap\ObjectIcons]])
	quest:SetTexCoord(1/8, 2/8, 1/8, 2/8)
	quest:SetSize(28 * .8, 28 * .8)
	quest:SetPoint('BOTTOMLEFT', -28 * .15, 0)
	frame.quest = quest

	local count = iconFrame:CreateFontString(nil, "OVERLAY")
	count:SetJustifyH"RIGHT"
	count:SetPoint("BOTTOMRIGHT", iconFrame, 2, 2)
	count:SetFont(font, fontsize, fontflag)
	count:SetShadowOffset(0,0)
	count:SetShadowColor(0, 0, 0, 1)
	count:SetText(1)
	frame.count = count

	local name = frame:CreateFontString(nil, "OVERLAY")
	name:SetJustifyH"LEFT"
	name:SetPoint("LEFT", frame, 5, 0)
	name:SetPoint("RIGHT", iconFrame, "LEFT")
	name:SetNonSpaceWrap(true)
	name:SetFont(font, fontsize, fontflag)
	name:SetShadowOffset(.8, -.8)
	name:SetShadowColor(0, 0, 0, 0)
	frame.name = name

	local drop = frame:CreateTexture(nil, "ARTWORK")
	drop:SetTexture[[Interface\\Buttons\\WHITE8x8]]

	drop:SetPoint("LEFT", icon, "RIGHT")
	drop:SetPoint("RIGHT", frame)
	drop:SetAllPoints(frame)
	drop:SetAlpha(.1)
	frame.drop = drop

	slots[id] = frame
	return frame
end

function Butsu:UpdateWidth()
	local maxWidth = 0
	for _, slot in next, _NS.slots do
		if(slot:IsShown()) then
			local width = slot.name:GetStringWidth()
			if(width > maxWidth) then
				maxWidth = width
			end
		end
	end

	self:SetWidth(math.max(maxWidth + 30 + 28, self.title:GetStringWidth() + 5))
end

function Butsu:AnchorSlots()
	local frameSize = math.max(28, 28)
	local iconSize = 28
	local shownSlots = 0

	local prevShown
	for i=1, #slots do
		local frame = slots[i]
		if(frame:IsShown()) then
			frame:ClearAllPoints()
			frame:SetPoint("LEFT", -10, 0)
			frame:SetPoint("RIGHT", -10, 0)
			if(not prevShown) then
				frame:SetPoint('TOPLEFT', self, -10, 0)
			else
				frame:SetPoint('TOP', prevShown, 'BOTTOM',0,1)
			end

			frame:SetHeight(frameSize)
			shownSlots = shownSlots + 1
			prevShown = frame
		end
	end

	local offset = self:GetTop() or 0
	self:SetHeight(math.max((shownSlots * frameSize + 2), 20))

	-- Reposition the frame so it doesn't move.
	-- local point, parent, relPoint, x, y = self:GetPoint()
	-- offset = offset - (self:GetTop() or 0)
	-- self:SetPoint(point, parent, relPoint, x, y + offset)
end
